import { arrayMoveMutable } from '@/library/libs/array-move'
/**
 * @description Clones the given `array`, moves the item to a new position in the new array, and then returns the new array. The given `array` is not mutated.
 * @param array - The array with the item to move.
 * @param fromIndex - The index of item to move. range [-array.length, array.length - 1]
 * @param toIndex - The index of where to move the item. range [-array.length, Number.Infinity]
 * @returns A new array with the item moved to the new position.
 *
 * @example
 ```
  const input = ['a', 'b', 'c']

  const array1 = arrayMoveImmutable(input, 1, 2)
  console.info(array1)
  // => ['a', 'c', 'b']

  const array2 = arrayMoveImmutable(input, -1, 0)
  console.info(array2)
  // => ['c', 'a', 'b']

  const array3 = arrayMoveImmutable(input, -2, -3)
  console.info(array3)
  // => ['b', 'a', 'c']

  const array4 = arrayMoveImmutable(input, -2, 1000)
  console.info(array4)
  // => ['a', 'c', 'b']

  const array5 = arrayMoveImmutable(input, -2, -1000)
  console.info(array5)
  // => ['a', 'c', 'b'] // 不会移动
 ```
 */
export const arrayMoveImmutable = <T extends unknown[]>(array: Readonly<T>, fromIndex: number, toIndex: number): T => {
  const newArray = [...array] as T
  arrayMoveMutable(newArray, fromIndex, toIndex)
  return newArray
}
